// 向服务器端发送请求 获取文章列表数据
$.ajax({
    type: 'get',
    url: '/posts',
    success: function (response) {
      let html = template('postsTpl', response);
      $('#postBox').html(html);
      let page = template('pageTpl', response);
      $('#page').html(page);
    }
});

// 分页功能
function changePage(page) {
    $.ajax({
      type: 'get',
      url: '/posts',
      data: {
        page: page
      },
      success: function (response) {
        let html = template('postsTpl', response);
        $('#postBox').html(html);
        let page = template('pageTpl', response);
        $('#page').html(page);
      }
    });
}

// 向服务器端发送请求 索要分类数据
$.ajax({
    type: 'get',
    url: '/categories',
    success: function (response) {
      let html = template('categoryTpl', {
        data: response
      });
      $('#categoryBox').html(html);
    }
});

// 当用户进行文章列表筛选的时候
$('#filterForm').on('submit', function () {
    let formData = $(this).serialize();
    $.ajax({
        type: 'get',
        url: '/posts',
        data: formData,
        success: function (response) {
            let html = template('postsTpl', response);
            $('#postBox').html(html);
            let page = template('pageTpl', response);
            $('#page').html(page);
        }
    });
    return false;
});

// 删除文章
$('#postBox').on('click', '.delete', function () {
    if (confirm('您确定要删除这篇文章吗？')) {
        let id = $(this).attr('data-id');
        $.ajax({
            type: 'delete',
            url: '/posts/' + id,
            success: function () {
                location.reload();
            }
        })
    }
})